<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (!($broker_id = $route->getAdminId()))
	goToPage("/admin/brokers");

$errors = array();

if ($route->POST("action") == 'edit')
{
	$city = UV::readInt($route->POST("city"));
	$name = UV::readMQ($route->POST("name"));
	$address = UV::readMQ($route->POST("address"));
	$phone = UV::readMQ($route->POST("phone"));
	$about = UV::readMQ($route->POST("about"));
	$donor = trim(UV::readMQ($route->POST("donor")));
	$cookie = trim(UV::readMQ($route->POST("cookie")));

	$query = "update `brokers` set `city_id` = '$city',`name` = '$name',`address` = '$address',`phone` = '$phone',`about` = '$about',`donor` = '$donor',`cookie`='$cookie' where `broker_id` = '$broker_id' limit 1";
	
	$update = $db->sendQuery($query);
	if ($update) {
		$db->sendQuery("delete from `partners` where `broker_id` = '$broker_id'");
		$companies = array();
		
		if ($route->POST("companies")){
			foreach ($route->POST("companies") as $c){
				if (!in_array($c,$companies)){
					$companies[] = $c;
				}
			}
		}
		
		if (count($companies) > 0){
			$query = "insert into `partners` values ";
			foreach ($companies as $key=>$company){
				$query .= "(NULL,'$broker_id','$company')";
				if ($key < count($companies) - 1)
					$query .= ",";
			}
			
			if (!$db->sendQuery($query))
				$errors[] = "Ошибка обновления партнеров";
		}
	} else {
		$errors[] = "Ошибка обновления записи";
	}
}

$broker = $db->getRow("select * from `brokers` where `broker_id` = '$broker_id' limit 1");
if (!$broker)
	goToPage("/admin/brokers");

loadPage("head.php");
?>
<script type='text/javascript'>
$(document).ready(function(){
	$("#add_company").click(function(){
		var select = "<tr><td><select name='companies[]' class='double_short'>"+$("#hidden_select").html()+"</select></td><td><input type='button' class='del' value='Удалить' /></td></tr>";
		$(this).parents("tr:eq(0)").before(select);
	})
	$(".del").live("click",function(){
		var parent = $(this).parents("tr:eq(0)");
		parent.css("background","#faa");
		var answer = confirm("Действительно удалить?");
		parent.css("background","");
		if (answer){
			parent.remove();
		}
	})
})
</script>
<h1>Редактирование брокера</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
	elseif ($route->POST("action") == 'edit')
		echo "<p class='success'>Успешно отредактировано<br /><br /></p>";
?>
<form method='POST' id="edit">
	<input type='hidden' name='action' value='edit' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='name'>Название</label></td>
			<td width='600px'><input type='text' id='name' name='name' value='<?php echo $broker['name']; ?>' /></td>
		</tr>
		<tr>
			<td><label for='city'>Город</label></td>
			<td>
				<select name='city' id='city' class='double_short'><?php
					$cities_list = $db->getTable("select `city_id`,`name` from `city` order by `name`");
					echo select_create($cities_list,"city_id","name",$broker['city_id']);
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='address'>Адрес</label></td>
			<td><input type='text' name='address' id='address' value='<?php echo $broker['address']; ?>' /></td>
		</tr>
		<tr>
			<td><label for='phone'>Телефон(ы)</label></td>
			<td><input type='text' name='phone' id='phone' value='<?php echo $broker['phone']; ?>' /></td>
		</tr>
		<tr>
			<td><label for='donor'>Страница-донор</label></td>
			<td>
				<input type='text' name='donor' id='donor' value='<?php echo $broker['donor']; ?>' />
			</td>
		</tr>
		<tr>
			<td><label for='cookie'>Cookie</label></td>
			<td>
				<input type='text' name='cookie' id='cookie' value='<?php echo $broker['cookie']; ?>' />
			</td>
		</tr>
		<tr>
			<td><label for='about'>О брокере</label></td>
			<td><textarea name='about' id='about'><?php echo $broker['about']; ?></textarea></td>
		</tr>
		<tr>
			<td>Сотрудничает с</td>
			<td>
				<table><?php
					$companies = $db->getTable("select `company_id`,`name` from `companies` where `head_office` = '0' order by `name`");
					$partners = $db->getColumn("select `company_id` from `partners` where `broker_id` = '$broker_id'");
					foreach ($partners as $partner){
						echo "<tr><td><select name='companies[]' class='double_short'>";
							echo select_create($companies,"company_id","name",$partner);
						echo "</select></td><td><input type='button' class='del' value='Удалить' /></td></tr>";
					}
					?><tr><td><input type='button' id='add_company' value="Добавить компанию" /></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Редактировать' /></td>
		</tr>
	</table>
	<select id='hidden_select' style='display:none'><?php
		echo select_create($companies,"company_id","name");
	?></select>
</form>

<?php loadPage("footer.php"); ?>